﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VLUSM_DataAccess.Repositories
{
    public class SinhVienRepository:GenericRepository<MainDataContainer>
    {
        public SinhVienRepository(string connectionString) : base(connectionString, "MainData") { }
        public SinhVienRepository(MainDataContainer context) : base(context) { }

        public SinhVien GetSingleByID(int IDFilter)
        {
            var query = this.GetFirst<SinhVien>(d => d.ID == IDFilter);
            return query;
        }

        public SinhVien GetSingleByMa(string MaFilter)
        {
            var query = this.GetFirst<SinhVien>(d => d.MaSinhVien == MaFilter);
            return query;
        }

        public SinhVien GetSingleByTen(string TenFilter)
        {
            var query = this.GetFirst<SinhVien>(d => d.TenSinhVien == TenFilter);
            return query;
        }

        public IEnumerable<SinhVien> GetAllByTen(string TenFilter)
        {
            var query = this.GetAll<SinhVien>().Where(d => d.TenSinhVien == TenFilter);
            return query;
        }

        public IEnumerable<SinhVien> GetAllByDonViID(int DonViIDFilter)
        {
            var query = this.GetAll<SinhVien>().Where(l => l.Lop.DonViID == DonViIDFilter);
            return query;
        }

        public IEnumerable<SinhVien> GetAllByKhoaHocID(int KhoaHocIDFilter)
        {
            var query = this.GetAll<SinhVien>().Where(d => d.KhoaHocID == KhoaHocIDFilter);
            return query;
        }

        public IEnumerable<SinhVien> GetAllByTenDonVi(string TenDonViIDFilter)
        {
            var query = this.GetAll<SinhVien>().Where(l => l.Lop.DonVi.TenGoi == TenDonViIDFilter);
            return query;
        }

        public IEnumerable<SinhVien> GetAllByTenKhoaHoc(string TenKhoaHocFilter)
        {
            var query = this.GetAll<SinhVien>().Where(l => l.KhoaHoc.TenGoi == TenKhoaHocFilter);
            return query;
        }
        
        public IEnumerable<SinhVien> GetAll()
        {
            var query = this.GetAll<SinhVien>();
            return query;
        }

        public void Save(SinhVien obj)
        {
            try
            {
                if (obj.ID != 0)
                {
                    this.Attach<SinhVien>(obj);
                }
                else
                {
                    this.Add<SinhVien>(obj);
                }
            }
            catch { }
        }

        public void Remove(SinhVien obj)
        {
            this.Delete<SinhVien>(obj);
        }

        public void Remove(int IDFilter)
        {
            var obj = this.GetSingleOrDefault<SinhVien>(s => s.ID == IDFilter);
            this.Delete<SinhVien>(obj);
        }

    }
}
